package com.wonyesoft.wuliu.acticity;

import java.util.HashMap;
import java.util.Map;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.StringUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserAddAddressActivity extends BaseActivity implements
		OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private Button address_info_baocun;
	private PreferencesCookieStore preferencesCookieStore = AppContent
			.getPreferencesCookieStore();
	private SharedPreferences share;
	private String key = "UserInfo";
	private String phone, address, realname, mark;
	private HttpUtils http = new HttpUtils();
	private String token;
	private EditText login_id, login_pwd;
	public static Gson gson = new Gson();
	private RequestParams params;
	private Editor editor;
	private EditText address_info_phone, address_info_name, address_lbs;
	
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_info);
		//保留做更改用
		mark = getIntent().getStringExtra("update");
		title = (TextView) findViewById(R.id.head_title);
		head_left = (ImageView) findViewById(R.id.head_left);
		address_lbs = (EditText) findViewById(R.id.address_lbs);
		address_info_phone = (EditText) findViewById(R.id.address_info_phone);
		address_info_name = (EditText) findViewById(R.id.address_info_name);
		address_info_baocun = (Button) findViewById(R.id.address_info_baocun);
		title.setText("添加地址");
		head_left.setOnClickListener(this);
		address_info_baocun.setOnClickListener(this);
		head_left.setVisibility(0);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.address_info_baocun:
			save();
			break;
		default:
			break;
		}
	}

	private void save() {
		phone = address_info_phone.getText().toString();
		realname = address_info_name.getText().toString();
		address = address_lbs.getText().toString();
		/*
		 * if (share.contains(key) && !share.getString(key, "").equals("")) {
		 * IntentUtil.start_activity(this, MainActivity.class); finish(); }
		 */
		// 判断输入
		if (StringUtils.isEmpty(realname)) {
			showLongToast("请输入发货人姓名");
			return;
		}
		if (StringUtils.isEmpty(address)) {
			showLongToast("请输入发货人地址");
			return;
		}

		if (StringUtils.isEmpty(phone)) {
			showLongToast("请输入发货人手机");
			return;
		}

		Map<String, Object> dataMap = new HashMap<String, Object>();
		JsonRequest jr = JsonRequest.getInit();
		dataMap.put("linkMan", realname);
		dataMap.put("address", address);
		dataMap.put("mobile", phone);
		System.out.println("realname:" + realname + "address" + address
				+ "phone:" + phone);
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(preferencesCookieStore);
		http.send(HttpRequest.HttpMethod.POST, GagApi.ADDADDRESS, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						// testTextView.setText("conn...");
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						if (isUploading) {

						} else {

						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						JsonResponse jrs = gson.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();
						if (state.getCode().equals(State.SUCEECCODE)) {
							showLongToast(state.getMsg());
							System.out.println("保存成功");
							finish();
						} else {
							System.out.println(state.getMsg());
							// ToastUtils.showLong(state.getMsg());
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {

						System.out.println(msg);
					}
				});

	}
	
	

}
